<?php

namespace FmBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CollectionType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('tags')
            ->add('submit', SubmitType::class, [
                'label' => '提交'
            ]);
        ;

        $builder->get('tags')->addModelTransformer(new CallbackTransformer(

            //拿出来的处理
            function($toEncode) {
                return base64_decode($toEncode, true);
            },

            //存进去的处理
            function($toDecode){

                return base64_encode($toDecode);
            }
        ));
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FmBundle\Entity\Collection'
        ));
    }
}
